package com.sjtuy.galtxtreader;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjtuy.galtxtreader.gal.GamingTextProvider;
import com.sjtuy.galtxtreader.gal.SLListAdapter;
import com.sjtuy.galtxtreader.gal.SLListItemView;
import com.sjtuy.galtxtreader.gal.TextProvider;

import java.io.File;

public class GalActivity extends Activity {
    TextProvider textProvider;
    File bookDir;

    RelativeLayout dialogLayout;
    RelativeLayout galCgLayout;
    TextView dialogView;
    TextView bookNameView;
    TextView chapterNameView;
    TextView indexView;

    TextView actionRewindingView;
    TextView actionSpeedingView;
    TextView actionSaveView;
    TextView actionLoadView;

    Handler handler;

    SLListItemView.SLDoer slDoer;
    SLListAdapter slListAdapter;
    AlertDialog slAlertDialog;

    private final int SPEEDING = 1;
    private final int REWINDING = 2;
    private final int AUTO = 2;
    private int currentWorkState;

    private int playingIndex =0;
    Runnable speedRunnable = new Runnable() {
        @Override
        public void run() {
            handler.removeMessages(0);
            speedSingle();
            if(currentWorkState==SPEEDING){
                handler.postDelayed(this,100);
            }
        }
    };
    Runnable rewindRunnable = new Runnable() {
        @Override
        public void run() {
            handler.removeMessages(0);
            rewindSingle();
            if(currentWorkState==REWINDING){
                handler.postDelayed(this,100);
            }
        }
    };
    Runnable playRunnable = new Runnable() {
        @Override
        public void run() {
            handler.removeMessages(0);
            dialogView.append(textProvider.getCurrentDialog(),playingIndex,playingIndex+1);
            playingIndex++;
            if(currentWorkState==0&&playingIndex< textProvider.getCurrentDialog().length()){
                handler.postDelayed(this,20);
            }else {
                playingIndex=0;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gal);
        currentWorkState=0;
        handler = new Handler();
        bookDir = new File(getIntent().getStringExtra("dir"));
        if(getIntent().getStringExtra("type").equals("play"))
            textProvider = new GamingTextProvider(bookDir,this);


        bookNameView = findViewById(R.id.galview_bookname);
        dialogView = findViewById(R.id.galview_dialog);
        dialogLayout = findViewById(R.id.dialog_layout);
        chapterNameView = findViewById(R.id.galview_chaptername);
        indexView = findViewById(R.id.galview_index);
        actionRewindingView = findViewById(R.id.galview_action_rewind);
        actionSpeedingView = findViewById(R.id.galview_action_speeding);
        actionLoadView = findViewById(R.id.galview_action_load);
        actionSaveView = findViewById(R.id.galview_action_save);
        galCgLayout = findViewById(R.id.galview_cg_layout);
        bookNameView.setText(bookDir.getName());

        slDoer=new SLListItemView.SLDoer() {
            @Override
            public void save(int socket) {
                textProvider.saveTo(socket);
                slListAdapter.notifyDataSetChanged();
            }

            @Override
            public void load(int socket) {
                if(textProvider.loadFrom(socket))
                    slAlertDialog.dismiss();
                refreshText();

            }
        };
        slListAdapter = new SLListAdapter(GalActivity.this,slDoer,textProvider);

        textProvider.loadFrom(0);
        refreshText();
        galCgLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentWorkState=0;
                handler.removeMessages(0);
                playSingle();

            }
        });
        actionSpeedingView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentWorkState = SPEEDING;
                handler.post(speedRunnable);
            }
        });
        actionRewindingView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentWorkState = REWINDING;
                handler.post(rewindRunnable);
            }
        });
        actionSaveView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slListAdapter.setMode(SLListItemView.SAVE_MODE);
                slAlertDialog=new AlertDialog.Builder(GalActivity.this).setTitle("Save")
                        .setAdapter(slListAdapter,null).create();
                slAlertDialog.show();
            }
        });
        actionLoadView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slListAdapter.setMode(SLListItemView.LODE_MODE);
                slAlertDialog=new AlertDialog.Builder(GalActivity.this).setTitle("Load")
                        .setAdapter(slListAdapter,null).create();
                slAlertDialog.show();
            }
        });

    }

    private void refreshText(){
        dialogView.setText(textProvider.getCurrentDialog());
        chapterNameView.setText(textProvider.getCurrentChapterName());
        indexView.setText(textProvider.getCurrentIndex());
    }
    private  void playSingle(){
        if(playingIndex ==0){
            textProvider.toNextDialog();
            chapterNameView.setText(textProvider.getCurrentChapterName());
            indexView.setText(textProvider.getCurrentIndex());
            dialogView.setText("");
            handler.post(playRunnable);
        }if(playingIndex !=0){
            playingIndex =0;
            refreshText();
        }
    }
    private void speedSingle(){
        textProvider.toNextDialog();
        refreshText();
    }
    private void rewindSingle(){
        textProvider.toPreviousDialog();
        refreshText();
    }
    @Override
    protected void onPause() {
        super.onPause();
        textProvider.saveTo(0);
    }


    class SaveListAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return null;
        }
    }
}